package com.fengycx.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fengycx.constants.Constants;
import com.fengycx.constants.GlobalExceptionCode;
import com.fengycx.mapper.DriverAuthMaterialMapper;
import com.fengycx.pojo.domain.Driver;
import com.fengycx.pojo.domain.DriverAuthMaterial;
import com.fengycx.pojo.domain.DriverMaterialAuthLog;
import com.fengycx.pojo.dto.DriverAuditDto;
import com.fengycx.pojo.dto.DriverAuthenticDto;
import com.fengycx.service.IDriverAuthMaterialService;
import com.fengycx.service.IDriverMaterialAuthLogService;
import com.fengycx.service.IDriverService;
import com.fengycx.utils.AssertUtil;
import com.fengycx.utils.BitStatesUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
 * <p>
 * 司机实名资料 服务实现类
 * </p>
 *
 * @author Cino
 * @since 2024-02-19
 */
@Service
public class DriverAuthMaterialServiceImpl extends ServiceImpl<DriverAuthMaterialMapper, DriverAuthMaterial> implements IDriverAuthMaterialService {

    @Autowired
    private IDriverService driverService;

    @Autowired
    private IDriverMaterialAuthLogService logService;

    @Override
    public void driverAudit(DriverAuditDto driverAuditDto) {
        //参数校验
        //业务校验
        //实名审核信息是否存在
        //实名审核信息状态是否为待审核
        DriverAuthMaterial driverAuthMaterial = super.getOne(new LambdaQueryWrapper<DriverAuthMaterial>()
                .eq(DriverAuthMaterial::getId, driverAuditDto.getDriverMaterialId()));

        AssertUtil.isTrue(driverAuthMaterial.getRealAuthStatus()==0,"审核对象不存在");

        Long driverId = driverAuthMaterial.getDriverId();
        Driver driver = driverService.getById(driverId);
        DriverMaterialAuthLog log = new DriverMaterialAuthLog();
        //3.业务实现
        //3.1修改审核状态
        if(driverAuditDto.getApprove()){
        driverAuthMaterial.setRealAuthStatus(Constants.RealAuth.APPROVED);
        //3.2修改司机位状态【通过】
        driver.setBitState(BitStatesUtil.addState(driver.getBitState(),BitStatesUtil.OP_REAL_AUTHENTICATIONED));
        }else {
        //3.2修改司机位状态【拒绝】
        driverAuthMaterial.setRealAuthStatus(Constants.RealAuth.REVOKE);
        driver.setBitState(BitStatesUtil.removeState(driver.getBitState(),BitStatesUtil.OP_REAL_AUTHENTICATING));
        }
        Date date = new Date();
        driverAuthMaterial.setAuditTime(date);
        super.updateById(driverAuthMaterial);
        driverService.updateById(driver);

        //3.3保存审核日志
        log.setAuthMaterialId(driverAuditDto.getDriverMaterialId());
        log.setAuditTime(date);
        log.setAuditUserId(StpUtil.getLoginIdAsLong());
        log.setRealAuthStatus(driverAuthMaterial.getRealAuthStatus());
        log.setAuditRemark(driverAuditDto.getAuditRemark());
        log.setCreateTime(date);
        log.setUpdateTime(date);
        log.setDeleted(false);
        log.setVersion(0);
        logService.save(log);
    }



    @Override
    public void saveDriverAuthentic(DriverAuthenticDto driverAuthenticDto) {
        //1.参数校验
        //2.业务校验
        //2.1判断司机是否实名，或者正在实名认证中
        long driverId = StpUtil.getLoginIdAsLong();
        System.out.println(driverId);
        List<Integer> authState = Arrays.asList(Constants.RealAuth.VERIFYING, Constants.RealAuth.APPROVED);
        List<DriverAuthMaterial> authMaterials = super.list(new LambdaQueryWrapper<DriverAuthMaterial>()
                .eq(DriverAuthMaterial::getDriverId, driverId)
                .in(DriverAuthMaterial::getRealAuthStatus, authState));
        AssertUtil.isTrue(authMaterials.size()==0, GlobalExceptionCode.SERVICE_ERROR);

        //保存实名信息
        DriverAuthMaterial driverAuthMaterial = new DriverAuthMaterial();
        BeanUtil.copyProperties(driverAuthenticDto, driverAuthMaterial);
        driverAuthMaterial.setDriverId(driverId);
        driverAuthMaterial.setCreateTime(new Date());
        driverAuthMaterial.setRealAuthStatus(0);
        super.save(driverAuthMaterial);

        //修改司机认证位状态
        Driver driver = driverService.getOne(new LambdaQueryWrapper<Driver>().eq(Driver::getId, driverId));
        driver.setBitState(BitStatesUtil.addState(driver.getBitState(), BitStatesUtil.OP_REAL_AUTHENTICATING));
        driverService.updateById(driver);

    }

    @Override
    public DriverAuthMaterial loadDriverAuthMaterial() {
        long driverId = StpUtil.getLoginIdAsLong();
        return super.getOne(new LambdaQueryWrapper<DriverAuthMaterial>()
                .eq(DriverAuthMaterial::getDriverId, driverId)
                .orderByDesc(DriverAuthMaterial::getCreateTime)
                .last("limit 1")//这里是（limit 0,1） 的缩写，取第一条，limit 8（0,7） 就是取前8条
        );
    }

}
